<?php
class Controller_Public extends Controller
{    
    public function before(){        
        parent::before();
    }
    
    public function action_index(){
        ;
    }
	
    //验证码action，直接调用public/verifycode显示验证码图片
	public function action_verifycode() {
		lib_validate::verifycode();
	}
	
	public function action_a_content_cache(){
		if (empty($_POST))
			echo (string)Session::instance()->get('a_content_cache');
		elseif (isset($_POST['a_content'])){
			if (strlen($_POST['a_content']) == 0)
				Session::instance()->delete('a_content_cache');
			else{
				$a_content_cache = (string)Session::instance()->get('a_content_cache');
				if (trim($a_content_cache) != trim($_POST['a_content']))
					Session::instance()->set('a_content_cache',$_POST['a_content']);
			}
		}
		//echo 'cache ok';
		exit;
	}
	
	public function action_rss(){
		$conn = MON::connect(get_object_vars(project::config('conndb')));
		
		$newest = MON::factory('article')->find()->sort(array('addTime'=>-1))->limit(10);
		header("Content-Type:text/xml; charset=utf-8");
		echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel>';
		echo '<title>吴啸的技术Collections</title>';
		echo '<link>http://www.php302.com</link>';
		echo '<description>php,javascript,linux,mysql,mongodb,sqlite</description>';
		
		foreach ($newest as $new){
			echo '<item>';
			echo '<title>',$new['title'],'</title>';
			echo '<link>',project::a_url($new),'</link>';
			echo '<description>';
			echo htmlspecialchars(common::cutstr(strip_tags(htmlspecialchars_decode($new['content'])),200));
			echo htmlspecialchars('<br /><br /><br />');
			echo '</description>';
			echo '<pubDate>',date('Y-m-d H:i:s',$new['addTime']),'</pubDate>';
			echo '</item>';
		}
		
		echo '</channel></rss>';
		exit;
	}
	
	public function action_sitemapxml(){
		$conn = MON::connect(get_object_vars(project::config('conndb')));
		header("Content-Type:text/xml; charset=utf-8");
		echo '<?xml version="1.0" encoding="UTF-8" ?>';
		echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
		
		echo '<url><loc>',lib_url::base(),'</loc>';
		echo '<lastmod>',date('Y-m-d\TH:i:s+08:00'),'</lastmod>';
		echo '<changefreq>Always</changefreq><priority>1.0</priority></url>';
		
		$articles = MON::factory('article')->find()->sort(array('addTime'=>-1));
		foreach ($articles as $a){
			echo '<url><loc>',project::a_url($a),'</loc>';
			echo '<lastmod>',date('Y-m-d\TH:i:s+08:00'),'</lastmod>';
			echo '<changefreq>daily</changefreq><priority>0.9</priority></url>';
		}
		
		echo '<url><loc>',project::tag_url('top10'),'</loc>';
		echo '<lastmod>',date('Y-m-d\TH:i:s+08:00'),'</lastmod>';
		echo '<changefreq>monthly</changefreq><priority>0.8</priority></url>';
		
		$tags = Model::factory('tag')->get_tags();
		foreach ($tags as $tag){
			echo '<url><loc>',project::tag_url($tag['tag']),'</loc>';
			echo '<lastmod>',date('Y-m-d\TH:i:s+08:00'),'</lastmod>';
			echo '<changefreq>daily</changefreq><priority>0.7</priority></url>';
		}
		
		$doc = Model::factory('article')->doc();
		foreach ($doc as $month=>$v){
			echo '<url><loc>',project::a_url($month,false),'</loc>';
			echo '<lastmod>',date('Y-m-d\TH:i:s+08:00'),'</lastmod>';
			echo '<changefreq>monthly</changefreq><priority>0.7</priority></url>';
		}
		
		echo '</urlset>';
		exit;
	}
	
	public function action_sitemaptxt(){
		$conn = MON::connect(get_object_vars(project::config('conndb')));
		header("Content-Type:text/plain; charset=utf-8");
		
		echo lib_url::base(),"\n";
		
		$articles = MON::factory('article')->find()->sort(array('addTime'=>-1));
		foreach ($articles as $a){
			echo project::a_url($a),"\n";
		}
		
		echo lib_url::base('tag/top10'),"\n";
		
		$tags = Model::factory('tag')->get_tags();
		foreach ($tags as $tag){
			echo project::tag_url($tag['tag']),"\n";
		}
		
		$doc = Model::factory('article')->doc();
		foreach ($doc as $month=>$v){
			echo project::a_url($month,false),"\n";
		}
		
		exit;
	}
}